﻿using System;
using System.Collections.Generic;
using Close.io.Client;
using Newtonsoft.Json;

namespace Close.io.API
{
	public class Lead : CloseIoUpdatedableObject , IIdentifiable
	{
		public Lead()
		{
			Tasks = new List<Task>();
		
			Addresses = new List<Address>(); 
			
			Contacts = new List<Contact>(); 
			
			Opportunities = new List<Opportunity>(); 

			Custom = new CustomFields();
			
		}

		public Lead(string displayName, string name, string status, string url, string description) :this()
		{
			
			DisplayName = displayName;
			
			Name = name;
			
			Status = status;

			Url = url;
			
			Description = description;
		}

		public void AddContact(Contact contact)
		{
			((List<Contact>)Contacts).Add(contact);
		}

		public void AddContact(string name, string title = null)
		{
			if (name == null) throw new ArgumentNullException("name");
			AddContact(new Contact(name, title));
		}

		public void AddAddress(Address address)
		{
			if (address == null) throw new ArgumentNullException("address");
			((List<Address>)Addresses).Add(address);
		}

		public void AddTask(Task task)
		{
			if (task == null) throw new ArgumentNullException("task");
			((List<Task>)Tasks).Add(task);
		}

		public void AddTask(DateTime? dueDate, string text, string assignedTo, bool? isComplete)
		{
			AddTask(new Task(dueDate, text, assignedTo, isComplete));
		}

		public void AddOpportunity(Opportunity opportunity)
		{
			if (opportunity == null) throw new ArgumentNullException("opportunity");
			((List<Opportunity>)Opportunities).Add(opportunity);
		}

		public void AddCustomField(string name, string value)
		{
			if (Custom.ContainsKey(name))
				Custom[name] = value;

			Custom.Add(name, value);
		}

		#region Properties
		[JsonProperty(PropertyName = "tasks")]
		private IEnumerable<Task> Tasks { get; set; }

		[JsonProperty(PropertyName = "display_name")]
		public string DisplayName { get; set; }


		[JsonProperty(PropertyName = "addresses")]
		public IEnumerable<Address> Addresses { get; set; }


		[JsonProperty(PropertyName = "name")]
		public string Name { get; set; }


		[JsonProperty(PropertyName = "contacts")]
		public IEnumerable<Contact> Contacts { get; set; }


		[JsonProperty(PropertyName = "status_label")]
		public string Status { get; set; }


		[JsonProperty(PropertyName = "status_id")]
		public string StatusId { get; set; }


		[JsonProperty(PropertyName = "html_url")]
		public string HtmlUrl { get; private set; }

		[JsonProperty(PropertyName = "custom")]
		public CustomFields Custom { get; set; }

		[JsonProperty(PropertyName = "url")]
		public string Url { get; set; }

		[JsonProperty(PropertyName = "opportunities")]
		public IEnumerable<Opportunity> Opportunities { get; set; }

		[JsonProperty(PropertyName = "description")]
		public string Description { get; set; } 
		#endregion

		public class Address
		{

			#region Properties
			[JsonProperty(PropertyName = "city")]
			public string City { get; set; }

			[JsonProperty(PropertyName = "country")]
			public string Country { get; set; }
			[JsonProperty(PropertyName = "zipcode")]

			public string ZipCode { get; set; }

			[JsonProperty(PropertyName = "label")]

			public string Label { get; set; }

			[JsonProperty(PropertyName = "state")]

			public string State { get; set; }

			[JsonProperty(PropertyName = "address_1")]
			public string Address1 { get; set; }

			[JsonProperty(PropertyName = "address_2")]
			public string Address2 { get; set; }
			#endregion

			public Address(string address1, string address2, string city, string zipCode, string state, LeadAddressType type, string country = null)
			{
				City = city;
				Country = country;
				ZipCode = zipCode;
				Label = Enum.GetName(typeof(LeadAddressType), type);
				State = state;
				Address1 = address1;
				Address2 = address2;
			}

			public Address()
			{

			}

		}

		public enum LeadAddressType
		{
			Business, Mailing, Other
		}
	}
}